package mk.jug.academy.controller;

import java.text.MessageFormat;
import java.util.Properties;
import java.util.logging.Logger;

import mk.jug.academy.factory.EventFactory;
import mk.jug.academy.model.Lecturer;
import mk.jug.academy.model.Page;
import mk.jug.academy.model.status.LecturerStatus;
import mk.jug.academy.service.EventService;
import mk.jug.academy.service.LecturerService;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * View layer coordinator for the {@link Lecturer} related functionality in the backend.
 *
 * @author Ice.Penov
 */
@Controller
public class LecturerAdminController {
	
	//TODO: Externalize this.
	public static final int itemsPerPage = 8;
	
	private static final Logger LOG = Logger.getLogger(LecturerAdminController.class.getName());
	
	private static final String PREVIEW_LECTURER = "preview.lecturer";
	private static final String PENDING_LECTURERS = "admin.lecturers.pending";
	private static final String APPROVED_LECTURERS = "admin.lecturers.approved";
	private static final String RETIRED_LECTURERS = "admin.lecturers.retired";
	private static final String REJECTED_LECTURERS = "admin.lecturers.rejected";
	
	@Autowired private LecturerService lecturerService;
	@Autowired private EventService eventService;
	@Autowired private Properties properties;
		
	@RequestMapping(value = "/admin/lecturers/signed_up.do")
	public String listSignedUp(ModelMap map, @RequestParam(value = "page", required = false) Integer page) {
		if (page == null) {
			page = 1;
		}
		Page<Lecturer> pagedLecturers = lecturerService.listLatestByStatus(page, itemsPerPage, LecturerStatus.SIGNED_UP);
		map.addAttribute("lecturers", pagedLecturers.getItems());
		map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedLecturers.getTotalCount()));
		map.addAttribute("page", page);
		map.addAttribute("showActions", "true");
		return PENDING_LECTURERS;
	}
	
	@RequestMapping("/admin/lecturers/approved.do")
	public String listApproved(ModelMap map, @RequestParam(value = "page", required = false) Integer page) {
		if (page == null) {
			page = 1;
		}
		Page<Lecturer> pagedLecturers = lecturerService.listLatestByStatus(page, itemsPerPage, LecturerStatus.ACTIVE);
		map.addAttribute("lecturers", pagedLecturers.getItems());
		map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedLecturers.getTotalCount()));
		map.addAttribute("page", page);
		map.addAttribute("showActions", "true");
		return APPROVED_LECTURERS;
	}
	
	@RequestMapping("/admin/lecturers/rejected.do")
	public String listRejected(ModelMap map, @RequestParam(value = "page", required = false) Integer page) {
		if (page == null) {
			page = 1;
		}
		Page<Lecturer> pagedLecturers = lecturerService.listLatestByStatus(page, itemsPerPage, LecturerStatus.REJECTED);
		map.addAttribute("lecturers", pagedLecturers.getItems());
		map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedLecturers.getTotalCount()));
		map.addAttribute("page", page);
		return REJECTED_LECTURERS;
	}
	
	@RequestMapping("/admin/lecturers/view.do")
	public String preview(ModelMap map, @RequestParam(value = "id") Long id) {
		map.addAttribute("lecturer", lecturerService.load(id));
		return PREVIEW_LECTURER;
	}
	
	@RequestMapping("/admin/lecturers/retired.do")
	public String listRetired(ModelMap map, @RequestParam(value = "page", required = false) Integer page) {
		if (page == null) {
			page = 1;
		}
		Page<Lecturer> pagedLecturers = lecturerService.listLatestByStatus(page, itemsPerPage, LecturerStatus.RETIRED);
		map.addAttribute("lecturers", pagedLecturers.getItems());
		map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedLecturers.getTotalCount()));
		map.addAttribute("page", page);
		return RETIRED_LECTURERS;
	}
	
	@RequestMapping(value = "/admin/lecturers/approve.do")
	public String approveLecturer(@RequestParam(value = "id") Long id, @RequestParam("redirectURL") String redirectURL) {
		Lecturer lecturer = lecturerService.load(id);
		lecturerService.acceptRegistration(lecturer);
		LOG.info("Accepted lecturer [" + lecturer + "]");
		eventService.createEvent(EventFactory.approveLecturerEvent(lecturer));
		String subject = properties.getProperty("activation.lecturer.email.subject");
		String body = MessageFormat.format(properties.getProperty("activation.lecturer.email.body"), new Object[] {lecturer.getDisplayName()});
		Utils.sendMail(new String[] {lecturer.getEmail(), lecturer.getDisplayName()}, subject, body);
		return "redirect:" + redirectURL;
	}

	@RequestMapping(value = "/admin/lecturers/reject.do")
	public String rejectLecturer(@RequestParam(value = "id") Long id, @RequestParam("redirectURL") String redirectURL) {
		Lecturer lecturer = lecturerService.load(id);
		lecturerService.rejectRegistration(lecturer);
		LOG.info("Rejected lecturer [" + lecturer + "]");
		return "redirect:" + redirectURL;
	}	
	
	@RequestMapping(value = "/admin/lecturers/retire.do")
	public String retireLecturer(@RequestParam(value = "id") Long id, @RequestParam("redirectURL") String redirectURL) {
		Lecturer lecturer = lecturerService.load(id);
		lecturerService.retireLecturer(lecturer);
		LOG.info("Retired lecturer [" + lecturer + "]");
		return "redirect:" + redirectURL;
	}	
	
}